<?php
class ControllerCommunityForum extends Controller {
    private $error = array();

    public function index() {
        $this->load->language('community/forum');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('community/forum');

        $this->getList();
    }

    public function insert() {
        $this->load->language('community/forum');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('community/forum');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_community_forum->addForum($this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->getForm();
    }

    public function update() {
        $this->load->language('community/forum');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('community/forum');

        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            if(isset($this->request->post['add_mod'])){
                if(!empty($this->request->post['forum_mod_name'])){
                    $result=$this->model_community_forum->addMod($this->request->get,$this->request->post);

                    if(!$result){
                        $this->error['warning'] = $this->language->get('error_user_not_exist');
                    }
                }
            }elseif(isset($this->request->post['remove_mod'])){
                $this->model_community_forum->removeMod($this->request->get,$this->request->post);
            }else{
                if($this->validateForm()){
                    $this->model_community_forum->editForum($this->request->get['forum_id'], $this->request->post);

                    $this->session->data['success'] = $this->language->get('text_success');

                    $this->redirect($this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL'));
                }
            }
        }

        $this->getForm();
    }

    public function delete() {
        $this->load->language('community/forum');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('community/forum');

        if (isset($this->request->post['selected']) && $this->validateDelete($this->request->post['selected'])) {
            foreach ($this->request->post['selected'] as $forum_id) {
                $this->model_community_forum->deleteForum($forum_id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->getList();
    }

    private function getList() {
        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['insert'] = $this->url->link('community/forum/insert', 'token=' . $this->session->data['token'], 'SSL');
        $this->data['delete'] = $this->url->link('community/forum/delete', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['forum_categories'] = array();
        $this->data['forums'] = array();
        $this->data['sub_forums'] = array();

        $results = $this->model_community_forum->getForumCategories();

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_edit'),
                'href' => $this->url->link('community/forum/update', 'token=' . $this->session->data['token'] . '&forum_id=' . $result['forum_id'], 'SSL')
            );

            $this->data['forum_categories'][] = array(
                'forum_id'      => $result['forum_id'],
                'forum_name'    => $result['forum_name'],
                'sort_order_id' => $result['sort_order_id'],
                'selected'      => isset($this->request->post['selected']) && in_array($result['forum_id'], $this->request->post['selected']),
                'action'        => $action
            );

            //forums
            $forumResults = $this->model_community_forum->getForums($result['forum_id']);

            foreach ($forumResults as $forumResult) {
                $action = array();

                $action[] = array(
                    'text' => $this->language->get('text_edit'),
                    'href' => $this->url->link('community/forum/update', 'token=' . $this->session->data['token'] . '&forum_id=' . $forumResult['forum_id'], 'SSL')
                );

                $this->data['forums'][$result['forum_id']][] = array(
                    'forum_id'      => $forumResult['forum_id'],
                    'forum_name'    => '&nbsp;&nbsp;&raquo;&nbsp;' . $forumResult['forum_name'],
                    'sort_order_id' => $forumResult['sort_order_id'],
                    'selected'      => isset($this->request->post['selected']) && in_array($forumResult['forum_id'], $this->request->post['selected']),
                    'action'        => $action
                );

                //subforums
                $subForumResults = $this->model_community_forum->getSubForums($forumResult['forum_id']);

                foreach ($subForumResults as $subForumResult) {
                    $action = array();

                    $action[] = array(
                        'text' => $this->language->get('text_edit'),
                        'href' => $this->url->link('community/forum/update', 'token=' . $this->session->data['token'] . '&forum_id=' . $subForumResult['forum_id'], 'SSL')
                    );

                    $this->data['sub_forums'][$forumResult['forum_id']][] = array(
                        'forum_id'      => $subForumResult['forum_id'],
                        'forum_name'    => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&raquo;&raquo;&nbsp;' . $subForumResult['forum_name'],
                        'sort_order_id' => $subForumResult['sort_order_id'],
                        'selected'      => isset($this->request->post['selected']) && in_array($subForumResult['forum_id'], $this->request->post['selected']),
                        'action'        => $action
                    );
                }
            }
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_no_results'] = $this->language->get('text_no_results');

        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_sort_order'] = $this->language->get('column_sort_order');
        $this->data['column_action'] = $this->language->get('column_action');

        $this->data['button_insert'] = $this->language->get('button_insert');
        $this->data['button_delete'] = $this->language->get('button_delete');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $this->template = 'community/forum_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    private function getForm() {
        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['tab_general'] = $this->language->get('tab_general');
        $this->data['tab_tag'] = $this->language->get('tab_tag');
        $this->data['tab_permission'] = $this->language->get('tab_permission');

        $this->data['text_none'] = $this->language->get('text_none');
        $this->data['text_default'] = $this->language->get('text_default');
        $this->data['text_forum'] = $this->language->get('text_forum');
        $this->data['text_category'] = $this->language->get('text_category');
        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');

        $this->data['entry_type'] = $this->language->get('entry_type');
        $this->data['entry_name'] = $this->language->get('entry_name');
        $this->data['entry_description'] = $this->language->get('entry_description');
        $this->data['entry_rule'] = $this->language->get('entry_rule');
        $this->data['entry_category'] = $this->language->get('entry_category');
        $this->data['entry_parent'] = $this->language->get('entry_parent');
        $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
        $this->data['entry_status'] = $this->language->get('entry_status');
        $this->data['entry_mod'] = $this->language->get('entry_mod');
        $this->data['entry_add_mod'] = $this->language->get('entry_add_mod');
        $this->data['entry_tag'] = $this->language->get('entry_tag');
        $this->data['entry_topic_category_status'] = $this->language->get('entry_topic_category_status');
        $this->data['entry_topic_category_displayed'] = $this->language->get('entry_topic_category_displayed');
        $this->data['entry_administrative_permission'] = $this->language->get('entry_administrative_permission');
        $this->data['entry_topic_announcement'] = $this->language->get('entry_topic_announcement');
        $this->data['entry_topic_global_announcement'] = $this->language->get('entry_topic_global_announcement');
        $this->data['entry_topic_featured'] = $this->language->get('entry_topic_featured');
        $this->data['entry_topic_sticky'] = $this->language->get('entry_topic_sticky');
        $this->data['entry_topic_move'] = $this->language->get('entry_topic_move');
        $this->data['entry_topic_lock'] = $this->language->get('entry_topic_lock');
        $this->data['entry_topic_solve'] = $this->language->get('entry_topic_solve');
        $this->data['entry_topic_unsolve'] = $this->language->get('entry_topic_unsolve');
        $this->data['entry_edit_lock'] = $this->language->get('entry_edit_lock');
        $this->data['entry_post_approve'] = $this->language->get('entry_post_approve');
        $this->data['entry_post_recycle_bin_clear'] = $this->language->get('entry_post_recycle_bin_clear');
        $this->data['entry_post_members_edit'] = $this->language->get('entry_post_members_edit');
        $this->data['entry_post_members_delete'] = $this->language->get('entry_post_members_delete');
        $this->data['entry_common_permission'] = $this->language->get('entry_common_permission');
        $this->data['entry_administrative_group'] = $this->language->get('entry_administrative_group');
        $this->data['entry_registered_member_group'] = $this->language->get('entry_registered_member_group');
        $this->data['entry_other_group'] = $this->language->get('entry_other_group');
        $this->data['entry_user_defined_group'] = $this->language->get('entry_user_defined_group');
        $this->data['entry_view_forum'] = $this->language->get('entry_view_forum');
        $this->data['entry_topic_view'] = $this->language->get('entry_topic_view');
        $this->data['entry_topic_publish'] = $this->language->get('entry_topic_publish');
        $this->data['entry_topic_reply'] = $this->language->get('entry_topic_reply');
        $this->data['entry_post_delete'] = $this->language->get('entry_post_delete');
        $this->data['entry_post_edit'] = $this->language->get('entry_post_edit');
        $this->data['entry_poll_publish'] = $this->language->get('entry_poll_publish');
        $this->data['entry_poll_edit'] = $this->language->get('entry_poll_edit');
        $this->data['entry_poll_vote'] = $this->language->get('entry_poll_vote');
        $this->data['entry_posting_approval'] = $this->language->get('entry_posting_approval');
        $this->data['entry_posting_bbcode'] = $this->language->get('entry_posting_bbcode');
        $this->data['entry_posting_bbcode_img'] = $this->language->get('entry_posting_bbcode_img');
        $this->data['entry_posting_bbcode_flash'] = $this->language->get('entry_posting_bbcode_flash');
        $this->data['entry_posting_bbcode_url'] = $this->language->get('entry_posting_bbcode_url');
        $this->data['entry_posting_smiles'] = $this->language->get('entry_posting_smiles');
        $this->data['entry_common_permission'] = $this->language->get('entry_common_permission');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');
        $this->data['button_add'] = $this->language->get('button_add');
        $this->data['button_remove'] = $this->language->get('button_remove');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['name'])) {
            $this->data['error_name'] = $this->error['name'];
        } else {
            $this->data['error_name'] = array();
        }

        if (isset($this->error['tag'])) {
            $this->data['error_tag'] = $this->error['tag'];
        } else {
            $this->data['error_tag'] = array();
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        if (!isset($this->request->get['forum_id'])) {
            $this->data['action'] = $this->url->link('community/forum/insert', 'token=' . $this->session->data['token'], 'SSL');
        } else {
            $this->data['action'] = $this->url->link('community/forum/update', 'token=' . $this->session->data['token'] . '&forum_id=' . $this->request->get['forum_id'], 'SSL');
        }

        $this->data['cancel'] = $this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['token'] = $this->session->data['token'];

        if (isset($this->request->get['forum_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $category_forum_info = $this->model_community_forum->getCategoryOrForumInfo($this->request->get['forum_id']);
        }

        $this->load->model('localisation/language');

        $this->data['languages'] = $this->model_localisation_language->getLanguages();

        if (isset($this->request->post['name'])) {
            $this->data['name'] = $this->request->post['name'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['name'] = $category_forum_info['forum_name'];
        } else {
            $this->data['name'] = array();
        }

        if (isset($this->request->post['description'])) {
            $this->data['description'] = $this->request->post['description'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['description'] = $category_forum_info['forum_desc'];
        } else {
            $this->data['description'] = array();
        }

        if (isset($this->request->post['rule'])) {
            $this->data['rule'] = $this->request->post['rule'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['rule'] = $category_forum_info['forum_rules'];
        } else {
            $this->data['rule'] = array();
        }

        $this->data['categories'] = $this->model_community_forum->getForumCategories();

        $parent_forums = $this->model_community_forum->getParentForums();

        // Remove own id from list
        if (!empty($category_forum_info)) {
            foreach ($parent_forums as $key => $parent_forum) {
                if ($parent_forum['forum_id'] == $category_forum_info['forum_id']) {
                    unset($parent_forums[$key]);
                }
            }
        }

        $this->data['parent_forums'] = $parent_forums;

        if (isset($this->request->post['type'])) {
            $this->data['type'] = $this->request->post['type'];
        } elseif (!empty($category_forum_info)) {
            $this->data['type'] = $category_forum_info['forum_type'];
        } else {
            $this->data['type'] = 0;
        }

        if (isset($this->request->post['category_id'])) {
            $this->data['category_id'] = $this->request->post['category_id'];
        } elseif (!empty($category_forum_info)) {
            $this->data['category_id'] = $category_forum_info['forum_cat_id'];
        } else {
            $this->data['category_id'] = 0;
        }

        if (isset($this->request->post['parent_id'])) {
            $this->data['parent_id'] = $this->request->post['parent_id'];
        } elseif (!empty($category_forum_info)) {
            $this->data['parent_id'] = $category_forum_info['parent_id'];
        } else {
            $this->data['parent_id'] = 0;
        }

        if (isset($this->request->post['sort_order'])) {
            $this->data['sort_order'] = $this->request->post['sort_order'];
        } elseif (!empty($category_forum_info)) {
            $this->data['sort_order'] = $category_forum_info['sort_order_id'];
        } else {
            $this->data['sort_order'] = 0;
        }

        if (isset($this->request->post['topic_category_enabled'])) {
            $this->data['topic_category_enabled'] = $this->request->post['topic_category_enabled'];
        } elseif (!empty($category_forum_info)) {
            $this->data['topic_category_enabled'] = $category_forum_info['topic_category_enabled'];
        } else {
            $this->data['topic_category_enabled'] = 1;
        }

        if (isset($this->request->post['topic_category_displayed'])) {
            $this->data['topic_category_displayed'] = $this->request->post['topic_category_displayed'];
        } elseif (!empty($category_forum_info)) {
            $this->data['topic_category_displayed'] = $category_forum_info['topic_category_displayed'];
        } else {
            $this->data['topic_category_displayed'] = 1;
        }

        if (isset($this->request->post['status'])) {
            $this->data['status'] = $this->request->post['status'];
        } elseif (!empty($category_forum_info)) {
            $this->data['status'] = $category_forum_info['forum_enabled'];
        } else {
            $this->data['status'] = 1;
        }

        if(isset($this->request->get['forum_id'])){
            $results=$this->model_community_forum->getForumMods($this->request->get['forum_id']);

            $this->data['forum_mods']=array();

            foreach($results as $result){
                $this->data['forum_mods'][]=array(
                    'mod_id'      =>      $result['mod_id'],
                    'mod_name'    =>      $result['mod_name']
                );
            }
        }

        if (isset($this->request->post['tag'])) {
            $this->data['tags'] = $this->request->post['tag'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['tags'] = $this->model_community_forum->getTopicTags($this->request->get['forum_id']);
        } else {
            $this->data['tags'] = array();
        }

        if (isset($this->request->post['administrative_permission'])) {
            $this->data['administrative_permissions'] = $this->request->post['administrative_permission'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['administrative_permissions'] = $this->model_community_forum->getAdministrativePermission($this->request->get['forum_id']);
        } else {
            $this->data['administrative_permissions'] = array();
        }

        if (isset($this->request->post['admin_common_permission'])) {
            $this->data['admin_common_permissions'] = $this->request->post['admin_common_permission'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['admin_common_permissions'] = $this->model_community_forum->getAdminCommonPermission($this->request->get['forum_id']);
        } else {
            $this->data['admin_common_permissions'] = array();
        }

        if (isset($this->request->post['member_common_permission'])) {
            $this->data['member_common_permissions'] = $this->request->post['member_common_permission'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['member_common_permissions'] = $this->model_community_forum->getMemberCommonPermission($this->request->get['forum_id']);
        } else {
            $this->data['member_common_permissions'] = array();
        }

        if (isset($this->request->post['other_group_common_permission'])) {
            $this->data['other_group_common_permissions'] = $this->request->post['other_group_common_permission'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['other_group_common_permissions'] = $this->model_community_forum->getOtherGroupCommonPermission($this->request->get['forum_id']);
        } else {
            $this->data['other_group_common_permissions'] = array();
        }

        if (isset($this->request->post['user_defined_group_common_permission'])) {
            $this->data['user_defined_group_common_permissions'] = $this->request->post['user_defined_group_common_permission'];
        } elseif (isset($this->request->get['forum_id'])) {
            $this->data['user_defined_group_common_permissions'] = $this->model_community_forum->getUserDefinedGroupCommonPermission($this->request->get['forum_id']);
        } else {
            $this->data['user_defined_group_common_permissions'] = array();
        }

        $this->template = 'community/forum_form.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function autocomplete() {
        $json = array();

        if (isset($this->request->get['filter_name'])) {
            $this->load->model('community/forum');

            $data = array(
                'filter_name' => $this->request->get['filter_name'],
                'start'       => 0,
                'limit'       => 50
            );

            $results = $this->model_community_forum->getUsers($data);

            foreach ($results as $result) {
                $json[] = array(
                    'user_id'    => $result['user_id'],
                    'username'   => html_entity_decode($result['username'], ENT_QUOTES, 'UTF-8'),
                );
            }
        }

        $sort_order = array();

        foreach ($json as $key => $value) {
            $sort_order[$key] = $value['username'];
        }

        array_multisort($sort_order, SORT_ASC, $json);

        $this->response->setOutput(json_encode($json));
    }

    private function validateForm() {
        if (!$this->user->hasPermission('modify', 'community/forum')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        foreach ($this->request->post['name'] as $language_id => $value) {
            if ((utf8_strlen($value) < 2) || (utf8_strlen($value) > 255)) {
                $this->error['name'][$language_id] = $this->language->get('error_name');
            }
        }

        if (isset($this->request->post['tag'])) {
            foreach($this->request->post['tag'] as $row_id => $tag){
                foreach ($tag['topic_category_title'] as $language_id => $topic_category_title) {
                    if ((utf8_strlen($topic_category_title) < 2) || (utf8_strlen($topic_category_title) > 255)) {
                        $this->error['tag'][$row_id][$language_id] = $this->language->get('error_tag_title');
                    }
                }
            }
        }

        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    private function validateDelete($forum_ids) {
        if (!$this->user->hasPermission('modify', 'community/forum')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        foreach($forum_ids as $forum_id){
            $result=$this->model_community_forum->validateDelete($forum_id);

            if($result[0]){
                $this->error['warning'] = $this->language->get('error_not_empty');
            }

            if($result[1]){
                $this->error['warning'] = $this->language->get('error_has_subforum');
            }
        }
        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}
?>